#pragma once
#ifndef TOOLS_H
#define TOOLS_H
#include <iostream>
#include <algorithm>
#include "psram_manager.h"
#include "log.h"
#include <SD.h>
#include <SPI.h>

#define PEAK 10000

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif // M_PI

using namespace std;

bool readIntFromSD(File file, int &intVar);
bool readDoubleFromSD(File file, double &doubleVar);
bool readWordFromSD(File file, int maxLength, char *cmd);
bool readLineFromSD(File file, int maxLength, char *cmd);

// 将计算特征写入文件
void writeToTxt(const char *filepath, int label, const float *character, int character_size);

// 实现简单的汉宁窗函数
float* hanningWindow(int size);

// determine whether file exist
bool fileExists(const std::string& filename);

// 实现简化的STFT函数
float*** simplifiedSTFT(const float* x, int x_size, int n_fft, int hop_length, const std::string &window, bool center, const std::string &mode);

// 实现简化的梅尔滤波器组计算
float** melFilterBank(int n_fft, int n_mel, int sample_rate);

// 实现简化的梅尔频谱图计算
float** melSpectrogram(const float *x, int x_size, int sample_rate, int n_fft, int n_hop, const std::string& win, bool center, const std::string& mode, float ref_power, int n_mel, int fmin, int fmax);

#endif // TOOLS_H